package com.googlecode.horncomb.web.spring.tags;

import javax.servlet.jsp.JspException;

import org.springframework.web.servlet.tags.form.AbstractHtmlElementTag;
import org.springframework.web.servlet.tags.form.TagWriter;

public class SpanTag extends AbstractHtmlElementTag {

	private static final long serialVersionUID = -9075749059070594607L;

	/**
	 * The HTML '<code>span</code>' tag.
	 */
	private static final String SPAN_TAG = "span";

	/**
	 * The {@link TagWriter} instance being used.
	 * <p>
	 * Stored so we can close the tag on {@link #doEndTag()}.
	 */
	private TagWriter tagWriter;

	@Override
	protected int writeTagContent(TagWriter tagWriter) throws JspException {
		tagWriter.startTag(SPAN_TAG);
		writeDefaultAttributes(tagWriter);
		tagWriter.forceBlock();
		this.tagWriter = tagWriter;
		return EVAL_BODY_INCLUDE;
	}

	/**
	 * Overrides {@link #getName()} to always return the empty string "",
	 * because the '<code>name</code>' attribute is not supported by the '
	 * <code>label</code>' tag.
	 * @return the value for the HTML '<code>name</code>' attribute
	 */
	protected String getName() throws JspException {
		// This also suppresses the 'id' attribute (which is okay for a
		// <label/>)
		return "";
	}

	/**
	 * Close the '<code>label</code>' tag.
	 * @return {@link javax.servlet.jsp.tagext.Tag#EVAL_PAGE}
	 */
	public int doEndTag() throws JspException {
		this.tagWriter.endTag();
		return EVAL_PAGE;
	}

	/**
	 * Disposes of the {@link TagWriter} instance.
	 */
	public void doFinally() {
		super.doFinally();
		this.tagWriter = null;
	}

	/**
	 * @return the tagWriter
	 */
	protected TagWriter getTagWriter() {
		return tagWriter;
	}
}
